/**
 *
 */

package lohmar.ashar.android.batterySaver.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;

/**
 * @author Mihai HARAS (Lohmar ASHAR) 2011
 */
public class BatteryView extends TextView {
    private float batteryLevel = 0;

    public BatteryView(Context context) {
        this(context, null);
    }

    public BatteryView(Context context, AttributeSet attrs) {
        /* com.android.internal.R.attr.textViewStyle = 0x01010084 */
        this(context, attrs, 0x01010084);
    }

    public BatteryView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setGravity(Gravity.CENTER);
        setTextColor(Color.WHITE);
        setShadowLayer((float) 5.3, 0, 0, Color.BLACK);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
    }

    public float getBatteryLevel() {
        return batteryLevel;
    }

    public void setBatteryLevel(float batteryLevel) {
        this.batteryLevel = batteryLevel;
        setText("" + (int) (batteryLevel * 100) + " %");
        // TODO identify if the setText always calls onSizeChanged
        prepareBg();
    }

    public void setSize(int w, int h) {
        setWidth(w);
        setHeight(h);
        measure(w, h);
        layout(0, 0, w, h);
        prepareBg();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        setWidth(w);
        setHeight(h);
        measure(w, h);
        layout(0, 0, w, h);
        prepareBg();
        super.onSizeChanged(w, h, oldw, oldh);
    }

    private int getColorPart(int colorA, int colorB, int colorC, float p) {
        int colorx;
        if (p < 0.5) {
            colorx = (int) (colorB * p * 2.0 + colorA * (0.5 - p) * 2.0);
        } else {
            colorx = (int) (colorC * (p - 0.5) * 2.0 + colorB * (1.0 - p) * 2.0);
        }
        return colorx;
    }

    private void prepareBg() {
        int rw = getWidth();
        int w = (int) Math.floor(rw * batteryLevel);
        int x = 0, y = 0;
        if (w == 0) {
            x = 3;
            y = 3;
        } else if (w < 4) {
            x = w;
            y = w - 1;
        } else if (batteryLevel < 1 && w >= rw - 4) {
            System.out.println("try to reduce");
            w = w - 2;
        }
        GradientDrawable gd1 = new GradientDrawable();
        gd1.setStroke(2, Color.WHITE);
        gd1.setCornerRadius(5f);
        int r = getColorPart(255, 255, 0, batteryLevel);
        int g = getColorPart(0, 255, 255, batteryLevel);
        int b = getColorPart(0, 0, 0, batteryLevel);
        gd1.setColor(Color.rgb(r, g, b));

        GradientDrawable gd2 = new GradientDrawable();
        gd2.setVisible(true, true);
        gd2.setCornerRadii(new float[] {
                x, y, 3f, 3f, 3f, 3f, x, y
        });
        gd2.setColor(Color.BLACK);
        GradientDrawable[] bg = new GradientDrawable[] {
                gd1, gd2
        };
        LayerDrawable ld = new LayerDrawable(bg);
        ld.setLayerInset(1, w + 2, 2, 2, 2);
        setBackgroundDrawable(ld);
    }

}
